<div class="tmd-doc">
<p></p>
<h1 class="tmd-header-1">
More Go Related Topics
</h1>
<p></p>
<div class="tmd-usual">
Go 101 articles mainly focus on syntax and semantics in Go. There are some other Go related topics which are not covered in Go 101. The remaining of the current article will make simple introductions to those topics and provide some web links for readers to dig more into them.
</div>
<p></p>
<h3 class="tmd-header-3">
Profiling, Benchmarks and Unit/Fuzzing Tests
</h3>
<p></p>
<div class="tmd-usual">
We can use <code class="tmd-code-span">go test</code> command in Go Toolchain to run tests and benchmarks. Test source file names must end with <code class="tmd-code-span">_test.go</code>. Go Toolchain also supports profiling Go programs. Please read the following articles for more details.
</div>
<ul class="tmd-list">
<li class="tmd-list-item">
<div class="tmd-usual">
<a href="https://github.com/golang/go/wiki/Performance">Profiling Go programs</a>.
</div>
</li>
<li class="tmd-list-item">
<div class="tmd-usual">
<a href="https://golang.org/pkg/testing/">The testing standard package</a>.
</div>
</li>
<li class="tmd-list-item">
<div class="tmd-usual">
<a href="https://blog.golang.org/examples">Write testable examples</a>.
</div>
</li>
<li class="tmd-list-item">
<div class="tmd-usual">
<a href="https://blog.golang.org/subtests">Using subtests and sub-benchmarks</a>.
</div>
</li>
<li class="tmd-list-item">
<div class="tmd-usual">
<a href="https://golang.org/cmd/go/#hdr-Test_packages"><code class="tmd-code-span">go test</code> command options</a>.
</div>
</li>
<li class="tmd-list-item">
<div class="tmd-usual">
<a href="https://go.dev/doc/fuzz/">Go Fuzzing</a>.
</div>
</li>
</ul>
<p></p>
<h3 class="tmd-header-3">
gccgo
</h3>
<p></p>
<div class="tmd-usual">
<a href="https://github.com/golang/gofrontend">gccgo</a> is another Go compiler maintained by the Go core team. It is mainly used to verify the correctness of the standard Go compiler (gc). We can use the <code class="tmd-code-span">-compiler=gccgo</code> build option in several Go Toolchain commands to use the gccgo compiler instead of the gc compiler. For example, <code class="tmd-code-span">go run -compiler=gccgo main.go</code>. This option requires the gccgo program is installed. Once the gccgo program is installed, we can also <a href="https://golang.org/doc/install/gccgo#Using_gccgo">use the <code class="tmd-code-span">gccgo</code> command directly to compile Go code</a>.
</div>
<p></p>
<p></p>
<h3 class="tmd-header-3">
The <code class="tmd-code-span">go/*</code> Standard Packages
</h3>
<p></p>
<div class="tmd-usual">
The <code class="tmd-code-span">go/*</code> standard packages provide functionalities of parsing Go source files, which are very useful to write custom Go tools. Please read <a href="https://github.com/golang/example/tree/master/gotypes"><code class="tmd-code-span">go/types</code>: The Go Type Checker</a> and <a href="https://golang.org/pkg/go/">package documentation</a> for how to use these packages.
</div>
<p></p>
<p></p>
<h3 class="tmd-header-3">
System Calls
</h3>
<p></p>
<div class="tmd-usual">
We can make system calls by call the functions exported by the <code class="tmd-code-span">syscall</code> standard package. Please beware that, different from other standard packages, the functions in the <code class="tmd-code-span">syscall</code> standard package are operating system dependent.
</div>
<p></p>
<h3 class="tmd-header-3">
Go Assembly
</h3>
<p></p>
<div class="tmd-usual">
Go functions can be implemented with Go assembly language. Go assembly language is a cross-architectures (though not 100%) assembly language. Go assembly language is often used to implement some functions which are critical for Go program execution performances.
</div>
<p></p>
<div class="tmd-usual">
For more details, please follow the following links.
</div>
<ul class="tmd-list">
<li class="tmd-list-item">
<div class="tmd-usual">
<a href="https://golang.org/doc/asm">A quick guide to Go's assembler</a>
</div>
</li>
<li class="tmd-list-item">
<div class="tmd-usual">
<a href="https://talks.golang.org/2016/asm.slide">The Design of the Go assembler</a>
</div>
</li>
</ul>
<p></p>
<h3 class="tmd-header-3">
cgo
</h3>
<p></p>
<div class="tmd-usual">
We can call C code from Go code, and vice versa, through the cgo mechanism. Please follow the following links for details.
</div>
<ul class="tmd-list">
<li class="tmd-list-item">
<div class="tmd-usual">
<a href="https://golang.org/cmd/cgo/">cgo official documentation</a>
</div>
</li>
<li class="tmd-list-item">
<div class="tmd-usual">
<a href="https://blog.golang.org/c-go-cgo">C? Go? Cgo!</a>
</div>
</li>
<li class="tmd-list-item">
<div class="tmd-usual">
<a href="https://github.com/golang/go/wiki/cgo">cgo on Go wiki</a>
</div>
</li>
</ul>
<p></p>
<div class="tmd-usual">
It is possible to use C++ libraries through cgo by wrapping C++ libraries as C functions.
</div>
<p></p>
<div class="tmd-usual">
Please note that using cgo in code may make it is hard to maintain cross-platform compatibility of Go programs, and the calls between Go and C code are some less efficient than Go-Go and C-C calls.
</div>
<p></p>
<h3 id="cross-platform-compiling" class="tmd-header-3">
Cross-Platform Compiling
</h3>
<p></p>
<div class="tmd-usual">
The standard Go compiler supports cross-platform compiling. By setting the <code class="tmd-code-span">GOOS</code> and <code class="tmd-code-span">GOARCH</code> environments before running the <code class="tmd-code-span">go build</code> command, we can build a Windows executable on a Linux machine, and vice versa. Please read the following articles for details.
</div>
<ul class="tmd-list">
<li class="tmd-list-item">
<div class="tmd-usual">
<a href="https://github.com/golang/go/wiki/WindowsCrossCompiling">Building windows go programs on linux</a>.
</div>
</li>
<li class="tmd-list-item">
<div class="tmd-usual">
<a href="https://golang.org/doc/install/source#environment">The current supported target operating systems and compilation architectures</a>.
</div>
</li>
</ul>
<p></p>
<div class="tmd-usual">
In particular, since Go 1.11, Go Toolchain starts to support WebAssembly as a new kind of GOARCH. Please read <a href="https://github.com/golang/go/wiki/WebAssembly">this wiki article</a> for details.
</div>
<p></p>
<p></p>
<h3 class="tmd-header-3">
Compiler Directives
</h3>
<p></p>
<div class="tmd-usual">
The standard Go compiler supports several <a href="https://golang.org/cmd/compile/#hdr-Compiler_Directives">compiler directives</a>. A directive appears as a comment line like <code class="tmd-code-span">//go:DirectiveName args</code>. For examples, we can use the <a href="https://blog.golang.org/generate">go:generate</a> directive to generate code and use the <a href="https://golang.org/pkg/embed/">go:embed</a> directive (introduced in Go 1.16) to embed some data files in code.
</div>
<p></p>
<p></p>
<h3 class="tmd-header-3">
Build Constraints (Tags)
</h3>
<p></p>
<div class="tmd-usual">
We can use <a href="https://golang.org/pkg/go/build/">build constraints</a> to let compilers build source files selectively (a.k.a., ignore some source files). A build constraint is also called a build tag. A build constraint can appear as a comment line like <code class="tmd-code-span">// +build constraints</code> or appear as the suffix in the base name of a source file. Please note: the new <a href="https://go.googlesource.com/proposal/+/master/design/draft-gobuild.md"><code class="tmd-code-span">//go:build</code>directive</a> <a href="https://github.com/golang/go/issues/41184">introduced in Go 1.17</a> will retire the old <code class="tmd-code-span">// +build</code> constraints lines eventually.
</div>
<p></p>
<p></p>
<h3 class="tmd-header-3">
More Build Modes
</h3>
<p></p>
<div class="tmd-usual">
The <code class="tmd-code-span">go build</code> command in Go Toolchain supports several build modes. Please run <code class="tmd-code-span">go help buildmode</code> to show the available build modes or read the explanations for <a href="https://golang.org/cmd/go/#hdr-Build_modes">-buildmode option</a> instead. Except the <span class="tmd-bold">default</span> build mode, the most used build mode may be the <span class="tmd-bold">plugin</span> build mode. We can use the functions in <a href="https://golang.org/pkg/plugin/">the <code class="tmd-code-span">plugin</code> standard package</a> to load and use the Go plugin files outputted by using the <span class="tmd-bold">plugin</span> build mode.
</div>
<p></p>
<p></p>
</div>
